﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;

namespace InitiativeTracker
{
    public partial class Form1 : Form
    {
        List<EntryHelper> Entries;
        public Form1()
        {
            InitializeComponent();

            List<string> EntryStrings = LoadEntryStrings();
            Entries = GenerateEntries(EntryStrings);
            ConformEnriesToLayout(ref Entries);
            AddToControls(ref Entries);

            this.PlayerCharacter.Checked = true;
        }

        private List<string> LoadEntryStrings()
        {
            List<string> entries = new List<string>();
            entries.Add("Name");
            //entries.Add("Description");
            //entries.Add("Level");
            //entries.Add("CombatRole");
            //entries.Add("InitiativeModifier");
            //entries.Add("HP");
            //entries.Add("Senses");
            //entries.Add("AC");
            //entries.Add("Vision");
            //entries.Add("Fortitude");
            //entries.Add("Reflex");
            //entries.Add("Will");
            //entries.Add("Sure");
            //entries.Add("Fire");
            entries.Add("Cool");
            entries.Add("Dude");
            return entries;
        }
        private List<EntryHelper> GenerateEntries(List<string> Entries)
        {
            List<EntryHelper> entries = new List<EntryHelper>();
            foreach (string entry in Entries)
            {
                entries.Add( new EntryHelper(entry,new Point(0,0)));
            }
            return entries;
        }
        private void ConformEnriesToLayout(ref List<EntryHelper> Entries)
        {
            Point startlocation = new Point(0, 5);
            foreach (EntryHelper entry in Entries)
            {
                entry.SetLocation(startlocation.X, startlocation.Y);
                startlocation = new Point(startlocation.X, startlocation.Y + 40);
            }
        }

        private void AddToControls(EntryHelper Entry)
        {
            this.panel.Controls.Add(Entry.LABEL);
            this.panel.Controls.Add(Entry.TEXTBOX);
        }

        private void AddToControls(ref List<EntryHelper> Entries)
        {
            foreach (EntryHelper entry in Entries)
            {
                this.panel.Controls.Add(entry.LABEL);
                this.panel.Controls.Add(entry.TEXTBOX);
            }
        }

        private void RemoveFromControls(ref EntryHelper Entry)
        {
            this.panel.Controls.Remove(Entry.LABEL);
            this.panel.Controls.Remove(Entry.TEXTBOX);
        }

        private void RemoveFromControls(ref List<EntryHelper> Entry)
        {
            foreach (EntryHelper entry in Entries)
            {
                this.panel.Controls.Remove(entry.LABEL);
                this.panel.Controls.Remove(entry.TEXTBOX);
            }
        }

        private void Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Title = "Save File";
            saveDialog.Filter = "XML Document | *.xml";
            saveDialog.ShowDialog();

            string character = "Character";

            foreach (EntryHelper box in Entries)
            {
                XMLHelper.Instance().AddProperty(character, box.LABEL.Text, box.TEXTBOX.Text);
            }

            if (EnemyCharacter.Checked)
            {
                XMLHelper.Instance().AddProperty(character, "Type", EnemyCharacter.Text);
            }
            else if (NonPlayableCharacter.Checked)
            {
                XMLHelper.Instance().AddProperty(character, "Type", NonPlayableCharacter.Text);
            }
            else if (PlayerCharacter.Checked)
            {
                XMLHelper.Instance().AddProperty(character, "Type", PlayerCharacter.Text);
            }

            if (saveDialog.FileName != "")
            {
                XMLHelper.Instance().SaveToFile(saveDialog.FileName);
            }
        }

        private void Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Title = "Open File";
            openDialog.Filter = "XML Document | *.xml";
            openDialog.ShowDialog();

            List<EntryHelper> temp = new List<EntryHelper>();
            if (openDialog.FileName != "")
            {
               List<XElement> characterStats = XMLHelper.Instance().ReadFromFile(openDialog.FileName);
               if (characterStats != null)
               {
                   for (int index = 0; index < Entries.Count; index++ )
                   {
                       XElement element = characterStats.Find(x => x.Name.ToString() == Entries[index].LABEL.Name);
                       if (element == null)
                       {
                           continue;
                       }
                       switch (element.Name.ToString())
                           {
                               case "Type":
                                   switch (element.Value)
                                   {
                                       case "PC":
                                           this.PlayerCharacter.Checked = true;
                                           break;
                                       case "Enemy":
                                           this.EnemyCharacter.Checked = true;
                                           break;
                                       case "NPC":
                                           this.NonPlayableCharacter.Checked = true;
                                           break;
                                       default:
                                           break;
                                   }
                                   break;
                               default:
                                    Entries[index].TEXTBOX.Text = element.Value;
                                    temp.Add(Entries[index]);
                                    Entries.RemoveAt(index);
                                    characterStats.Remove(element);
                                    index--;
                                   break;
                           }
                   }
                   panel.Controls.Clear();
                   Entries.Clear();
                   Entries.AddRange(temp);
                   foreach (XElement elem in characterStats)
                   {
                       if (elem.Name == "Type") continue;
                       EntryHelper entry = new EntryHelper();
                       entry.SetLabel(elem.Name.ToString());
                       entry.TEXTBOX.Text = elem.Value.ToString();
                       Entries.Add(entry);
                   }
                   AddToControls(ref Entries);
                   ConformEnriesToLayout(ref Entries);
               }
            }
        }

        private void CreateStat_Click(object sender, EventArgs e)
        {
            CreateNewStat newStat = new CreateNewStat();
            newStat.ConfirmationSubscription((name, value) =>
            {
                EntryHelper temp = new EntryHelper(name.TEXTBOX.Text, new Point());
                temp.TEXTBOX.Text = value.TEXTBOX.Text;
                AddToControls(temp);
                Entries.Add(temp);
                ConformEnriesToLayout(ref Entries);
            });
            newStat.ShowDialog();
        }

    }
}
